<?php

namespace MLM\Model\Agent;

use MLM\Model\Agent;

class Storage extends \MLM\Model\Generic\Db\Storage {

    
    public function getSuplierList($key){
        
        $sql = "select * from supliers order by suplier_name";

        /*$sql = "select m.member_id as suplier_id, m.member_name as suplier_name,
                contact_city, contact_province
                from members m, product_stock p, member_contact mc, contact c
                where p.member_id = m.member_id and qty > 0
                and mc.member_id = m.member_id and c.contact_id = mc.contact_id
                order by member_name, contact_city";
         *
         */

        $stmt = $this->_db->query($sql);
        $res = $stmt->execute(array($key));
        

        return $res;
    }

    public function getAgentList($isAgent, $type = ""){
        /*
		if($type == 2){
			// agent list 
			
			$sql = "select member_reff_id, member_name, member_code from members where member_reff_id is not null";
		}else{
			$sql = "select member_reff_id, member_name, member_code from members where member_id = '100' order by member_name";
		}
         *
         */
	
        if ($isAgent == "" || $type == 2){ //Jika Bukan Agen
            $sql = "select * from members where member_reff_id is not null and member_type_id = 2 ";

            if ($isAdmin == ""){
                $sql .= " and member_id <> '100' ";
            }

            $sql .= " order by member_name ";

        }else // Jika Agen
            $sql = "select * from members where member_id = '100' order by member_name";
		
        $stmt = $this->_db->query($sql);
        $res = $stmt->execute(array($key));
        
        return $res;

    }

    public function getProductBySuplier($suplier, $agent){

        /*$sql = "select * from product p, product_price pp, product_stock ps
                where pp.product_id = p.product_id and ps.product_id = p.product_id 
                and ps.member_id = ?";
         *
         */

        $sql = "select * from product p, product_price pp, product_stock ps
                where pp.product_id = p.product_id and ps.product_id = p.product_id
                and p.suplier_id = ? and ps.member_id = ? and pp.start_dat <= now() and IFNULL(end_dat,now())  >= now() ";

        $stmt = $this->_db->query($sql);
        $res = $stmt->execute(array($suplier, $agent));

        return $res;
    }

    public function savePaymentConfirm($dataPayment){
        

        $sql = "INSERT INTO payment_confirm
                (trans_header_id, pay_date, pay_method, from_account_id, to_account_id, transfer_amount, notes)
                VALUES (?, STR_TO_DATE(?, '%d/%m/%Y'), ?, ?, ?, ?, ?);
                ";

        $stmt = $this->_db->query($sql);
        $result = $stmt->execute($dataPayment);

        return $result;
    }

    public function updateTransStatus($data, $where){

        $sql = $this->update()
                    ->table('trans_header')
                    ->set($data)
                    ->where($where);

        $result = $this->execute($sql);

        return $result;

    }


    public function getSNStockByAgent($agent){


        $sql = "select count(1) as total
                from sn
                where agent_id = ?
                and sn_status = 2
                and member_type = 'AGENT'
                and mitra_id is null";

        $stmt = $this->_db->query($sql);
        $res = $stmt->execute(array($agent));

        return $res;
    }

}
